#include "SocketConnPtr.h"
#include "SocketConnPool.h"

LoggerPtr SocketConnPtr::_logger = Logger::getLogger("SocketConnPtr");

SocketConnPtr::SocketConnPtr() { _fd = -1; _index = -1; _pool = NULL; _is_network_error = false; }

int SocketConnPtr::assign(int fd, int index, SocketConnPool * pool) {
	if (fd < 0 || index < 0 || NULL == pool) {
		LOG4CXX_ERROR(_logger, "assign error, invalid args");
		return -1;
	}
	if (_fd >= 0) {
		LOG4CXX_ERROR(_logger, "could not assgin a SocketConnPtr twice");
		return -2;
	}
	_fd = fd;
	_index = index;
	_pool = pool;
	return 1;
}

SocketConnPtr::~SocketConnPtr() {
	if (_fd > 0) _pool->release(_index, _is_network_error);
}

int SocketConnPtr::get() { return _fd; }

int SocketConnPtr::set_network_error(bool b) { _is_network_error = b; return 1; }

int SocketConnPtr::is_network_error() { return _is_network_error; }
	
